package com.qf.controller;

import com.qf.entity.Product;
import com.qf.entity.Shop;
import com.qf.entity.User;
import com.qf.entity.custom.ProductCustom;
import com.qf.entity.custom.ShopCustom;
import com.qf.entity.custom.UserSearchCustom;
import com.qf.service.ProductService;
import com.qf.service.ShopCustomService;
import com.qf.service.UserSearchService;
import com.qf.tools.JavaWebToken;
import com.qf.tools.ResponseUtils;
import com.qf.tools.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: ZhangYubin
 * @date: 2020/7/24 15:53
 * @description:
 */
@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private ProductService productService;
    @Autowired
    private ShopCustomService shopCustomService;
    @Autowired
    private UserSearchService userSearchService;

    /**
     * 搜索页面：登录和未登录状态搜索接口
     * @param userSearch userSearch实体类接收json字符串
     * @return 返回ResultEntity类型的成功或者失败信息并选择是否需要携带数据
     * @throws Exception
     */
    @RequestMapping(value = "/getShopProductBySearch", method = RequestMethod.POST)
    public ResultEntity getShopProductBySearch(@RequestBody UserSearchCustom userSearch) throws Exception {
        String searchwords = userSearch.getSearchwords();
        String token = userSearch.getToken();
        Map<String, Object> map = JavaWebToken.parserJavaWebToken(token);
        Integer userId = (Integer) map.get("userId");
        if (searchwords != null && searchwords != "") {
            List<Product> productList = productService.selecProductByNameKey(searchwords);
            List<Shop> shops = shopCustomService.selecShopByNameKey(searchwords);

            if (userId != null){
                if (productList != null | shops != null){
                    HashMap voList1 = new HashMap();
                    for (Product p : productList) {
                        Integer productId = p.getProductId();
                        voList1.put("productId",productId);
                    }
                    for (Shop s :shops) {
                        Integer shopId = s.getShopId();
                        voList1.put("shopId",shopId);
                    }
                    userSearchService.insertUserSearchByCount(voList1,userId);
                }
            }
            if (productList != null | shops != null){
                List voList = new ArrayList<>();
                for (Product p : productList) {
                    voList.add(p);
                }
                for (Shop s :shops) {
                    voList.add(s);
                }
                return ResultEntity.success(voList);
            }else {
                return ResponseUtils.responseClinet(false);
            }
        }else{
            return ResponseUtils.responseClinet(false);
        }
    }

    /**
     * 搜索页面：显示热门搜索词接口
     * @return 返回ResultEntity类型的成功或者失败信息并选择是否需要携带数据
     * @throws Exception
     */
    @RequestMapping(value = "/getShopProductSearch", method = RequestMethod.POST)
    public ResultEntity getShopProductSearch() throws Exception {
            List<ShopCustom> shopOrderByCount = shopCustomService.getShopOrderByCount();
            List<ProductCustom> productOrderByCount = productService.getProductOrderByCount();
        if (shopOrderByCount != null | productOrderByCount != null){
            List voList = new ArrayList<>();
            for (Product p : productOrderByCount) {
                voList.add(p);
            }
            for (Shop s :shopOrderByCount) {
                voList.add(s);
            }
            return ResultEntity.success(voList);
        }else {
            return ResponseUtils.responseClinet(false);
        }
    }

    /**
     * 搜索页面：用户历史搜索记录
     * @param user user实体类接收json字符串
     * @return 返回ResultEntity类型的成功或者失败信息并选择是否需要携带数据
     * @throws Exception
     */
    @RequestMapping(value = "/getUserSearchBySearch", method = RequestMethod.POST)
    public ResultEntity getUserSearchBySearch(@RequestBody User user) throws Exception {
        String token = user.getToken();
        Map<String, Object> map = JavaWebToken.parserJavaWebToken(token);
        Integer userId = (Integer) map.get("userId");
        if (userId != null){
            List<UserSearchCustom> userSearchCustoms = userSearchService.selectAllProductByCount(userId);
            List<UserSearchCustom> userSearchCustoms1 = userSearchService.selectAllShopByCount(userId);
            if (userSearchCustoms != null | userSearchCustoms1 != null){
                List voList = new ArrayList<>();
                for (UserSearchCustom p : userSearchCustoms) {
                    voList.add(p);
                }
                for (UserSearchCustom s :userSearchCustoms1) {
                    voList.add(s);
                }
                return ResultEntity.success(voList);
            }else {
                return ResponseUtils.responseClinet(false);
            }
        }else {
            return ResponseUtils.responseClinet(false);
        }
    }

}
